package com.eagle.spring.reactive.loader;

import java.util.concurrent.CompletableFuture;

public class CombineDataLoader extends DataLoader {
	protected void doLoad() {
		CompletableFuture<String> finalResult = runConfigurations()
				.thenCombineAsync(runUsers(), (f1, f2) -> {
					return combineResult(f1, f2);
				})
				.thenCombineAsync(runOrders(), (f1, f2) -> {
					return combineResult(f1, f2);
				});
		try {
			System.out.println("final result=> "+finalResult.get());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private CompletableFuture<String> runConfigurations() {
		return  CompletableFuture.supplyAsync(() -> {
			return super.loadConfigurations();
		});
	}

	private CompletableFuture<String> runUsers() {
		return  CompletableFuture.supplyAsync(() -> {
			return super.loadUsers();
		});
	}

	private CompletableFuture<String> runOrders() {
		return  CompletableFuture.supplyAsync(() -> {
			return super.loadOrders();
		});
	}

	private String combineResult(String a, String b){
		return a+b;
	}

	public static void main(String[] args) {
		new CombineDataLoader().load();
	}
}
